package org.istt.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.HashCodeBuilder;


/**
 * Menu entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name="TBL_MENU"
    ,schema="dbo"
    ,catalog="onegate"
)

public class Menu extends BaseObject<Long> implements java.io.Serializable {


    // Fields    

     /**
	 * 
	 */
	private static final long serialVersionUID = -4764883418576983623L;
	private Long id;
     private String formName;
     private String name;
     private Integer position;
     private Boolean isActive;
     private String url;
     private Boolean isFather;
     private Set<MenuTree> menuTrees = new HashSet<MenuTree>(0);
     private Set<Group> groups = new HashSet<Group>(0);


    // Constructors

    /** default constructor */
    public Menu() {
    }
    
    // Property accessors
    @Id @GeneratedValue(strategy=IDENTITY)
    
    @Column(name="ID", unique=true, nullable=false)

    public Long getId() {
        return this.id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    @Column(name="FORM_NAME")

    public String getFormName() {
        return this.formName;
    }
    
    public void setFormName(String formName) {
        this.formName = formName;
    }
    
    @Column(name="NAME")

    public String getName() {
        return this.name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Column(name="POSITION")

    public Integer getPosition() {
        return this.position;
    }
    
    public void setPosition(Integer position) {
        this.position = position;
    }
    
    @Column(name="IS_ACTIVE")

    public Boolean getIsActive() {
        return this.isActive;
    }
    
    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }
    
    @Column(name="URL", length=250)

    public String getUrl() {
        return this.url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    
    @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.LAZY, mappedBy="menu")
    public Set<MenuTree> getMenuTrees() {
        return this.menuTrees;
    }
    
    public void setMenuTrees(Set<MenuTree> menuTrees) {
        this.menuTrees = menuTrees;
    }
    
//    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "menus")
//    @ManyToMany(cascade ={CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.LAZY, mappedBy = "menus")
    @ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
	@JoinTable(name = "TBL_MENU_GROUP", schema = "dbo", catalog = "onegate", joinColumns = { @JoinColumn(name = "MENU_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "GROUP_ID", nullable = false, updatable = false) })
    public Set<Group> getGroups() {
        return this.groups;
    }
    
    public void setGroups(Set<Group> groups) {
        this.groups = groups;
    }


	public void setIsFather(Boolean isFather) {
		this.isFather = isFather;
	}

	@Column(name="IS_FATHER")
	public Boolean getIsFather() {
		return isFather;
	}
	
	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this, new String[]{"menuTrees"});
	}


	@Override
	public String toString() {
		
		return new StringBuilder("id = ").append(id)
		.append("name = ").append(name)
		.toString();
	}


	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (!(o instanceof Menu)) return false;

		final Menu ps = (Menu) o;
        if (!getName().equals(ps.getName())) return false;
        if (!getPosition().equals(ps.getPosition())) return false;
        if (!getUrl().equals(ps.getUrl())) return false;
        
        return true;
	}
}